import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class addAppliance extends HttpServlet
{
	public static dbapi conn;
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{ 
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		out.println(
		"<img src=\"http://www.carterservices.com/wp-content/uploads/2009/08/appliances-stainless-steel-lg2.jpg\" width=\"300\" height=\"278\" border=\"0\" alt=\"Appliances\" />"+
		"<p style=\"font-size:28pt;\"><b>Add an Appliance</b></p>"+
		"<html><head></head>"+
		"<body>"+
		"<form name=\"addAppliance\" action=\"/project/addAppliance\" method=\"post\">"+
		"<table><input type=\"hidden\" name=\"HOMEID\" value="+id+">"+
		"<tr><td>Maker:</td>"+
		"<td><input type=\"text\" name=\"MAKER\"></td></tr>"+
		"<tr><td>Type:</td>"+
		"<td><input type=\"text\" name=\"TYPE\"></td></tr>"+
		"</table>"+
		"<input type=\"submit\">"+
		"</form>"+
		"<body></html>");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		conn = new dbapi();
		conn.connect();
		if(conn.connected)
			out.println("connected via dbapi <br>");
		else
		{
			out.println("CANT CONNECT! <br>");
			out.println(conn.msg + "<br>");
			return;
		}
		String type = request.getParameter("TYPE");
		String homeID = request.getParameter("HOMEID");
		String maker = request.getParameter("MAKER");			   boolean worked = conn.addAppliance(homeID, type, maker);
		if (!worked)
		{
			out.println(conn.msg);
		}else{
			out.println("<header><meta http-equiv=\"refresh\" content=\"2;houseDetails?id="+homeID+"\"></header>");
		}
		return;
	}
}
